/*
 * Created by JFormDesigner on Sun May 18 23:00:32 GMT+08:00 2025
 */

package com.soft.bams.client;

import com.soft.bams.entity.Account;
import com.soft.bams.exception.NotFoundException;
import com.soft.bams.exception.RegisterException;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author wzs
 */
public class RegisterPanel extends JPanel {

    private ATMClient atm;

    public RegisterPanel(ATMClient atm) {
        this.atm = atm;
        initComponents();
    }

    //在注册界面中，点击取消按钮，跳回MainPanel界面
    private void back(ActionEvent e) {
        //获取当前MainPanel的父窗口（ATMClient JFrame） ->  获取它的布局方式  -> 跳转
        CardLayout cardLayout = (CardLayout)this.getParent().getLayout();
        //跳转至主界面
        cardLayout.show(this.getParent() , "mainPanel");
    }

    //提交注册表单
    private void regist(ActionEvent e) {
        // 收集表单中的数据
        String name = nameField.getText();
        String personId = personIdField.getText();
        String email = emailField.getText();
        String password = new String(passwordField.getPassword());
        String repassword = new String(repasswordField.getPassword());
        int type = comboBox.getSelectedIndex();

        // 调用Bank银行开户业务
        try {
            Account acc = atm.bank.register(password, repassword, name, personId, email, type);
            //开户成功
            if(acc!=null){
                // 1.初始化BusinessPanel界面
                atm.acc = acc;
                atm.businessPanel.initBusiness();

                // 2.跳转至BusinessPanel界面
                CardLayout cardLayout = (CardLayout)this.getParent().getLayout();
                cardLayout.show(this.getParent() , "businessPanel");

                // 3.清空注册界面数据
                clear();
            }
        } catch (NotFoundException | RegisterException ex) {
            //开户失败
            // 1.弹窗提醒失败信息
            JOptionPane.showMessageDialog(null, ex.getMessage());
            // 2.清空密码相关的输入框
            passwordField.setText("");
            repasswordField.setText("");
        }
    }

    //重置注册表单
    private void reset(ActionEvent e) {
        clear();
    }

    //清除注册界面
    public void clear(){
        nameField.setText("");
        personIdField.setText("");
        addrField.setText("");
        emailField.setText("");
        passwordField.setText("");
        repasswordField.setText("");
        //下拉菜单 默认选中储蓄账户
        comboBox.setSelectedIndex(0);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        titleLabel = new JLabel();
        backButton = new JButton();
        typeLabel = new JLabel();
        nameLabel = new JLabel();
        pwdLabel = new JLabel();
        repwdLabel = new JLabel();
        personIdLabel = new JLabel();
        addrLabel = new JLabel();
        emailLabel = new JLabel();
        comboBox = new JComboBox<>();
        nameField = new JTextField();
        personIdField = new JTextField();
        addrField = new JTextField();
        emailField = new JTextField();
        passwordField = new JPasswordField();
        repasswordField = new JPasswordField();
        registButton = new JButton();
        resetButton = new JButton();

        //======== this ========
        setLayout(null);

        //---- titleLabel ----
        titleLabel.setText("\u6ce8 \u518c \u754c \u9762");
        titleLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 20));
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(titleLabel);
        titleLabel.setBounds(140, 35, 185, 45);

        //---- backButton ----
        backButton.setText("\u53d6\u6d88");
        backButton.addActionListener(e -> back(e));
        add(backButton);
        backButton.setBounds(325, 470, 105, 35);

        //---- typeLabel ----
        typeLabel.setText("\u8d26 \u6237 \u7c7b \u578b");
        typeLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(typeLabel);
        typeLabel.setBounds(75, 110, 95, 25);

        //---- nameLabel ----
        nameLabel.setText("\u7528 \u6237 \u540d");
        nameLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(nameLabel);
        nameLabel.setBounds(75, 160, 95, 25);

        //---- pwdLabel ----
        pwdLabel.setText("\u5bc6    \u7801");
        pwdLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(pwdLabel);
        pwdLabel.setBounds(75, 210, 95, 25);

        //---- repwdLabel ----
        repwdLabel.setText("\u786e \u8ba4 \u5bc6 \u7801");
        repwdLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(repwdLabel);
        repwdLabel.setBounds(75, 260, 95, 25);

        //---- personIdLabel ----
        personIdLabel.setText("\u8eab \u4efd \u8bc1 \u53f7");
        personIdLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(personIdLabel);
        personIdLabel.setBounds(75, 310, 95, 25);

        //---- addrLabel ----
        addrLabel.setText("\u8be6 \u7ec6 \u5730 \u5740");
        addrLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(addrLabel);
        addrLabel.setBounds(75, 360, 95, 25);

        //---- emailLabel ----
        emailLabel.setText("\u90ae    \u7bb1");
        emailLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(emailLabel);
        emailLabel.setBounds(75, 410, 95, 25);

        //---- comboBox ----
        comboBox.setModel(new DefaultComboBoxModel<>(new String[] {
            "\u50a8\u84c4\u8d26\u6237",
            "\u4fe1\u7528\u8d26\u6237",
            "\u53ef\u8d37\u6b3e\u50a8\u84c4\u8d26\u6237",
            "\u53ef\u8d37\u6b3e\u4fe1\u7528\u8d26\u6237"
        }));
        add(comboBox);
        comboBox.setBounds(200, 105, 160, 35);
        add(nameField);
        nameField.setBounds(200, 155, 160, 35);
        add(personIdField);
        personIdField.setBounds(200, 305, 160, 35);
        add(addrField);
        addrField.setBounds(200, 355, 160, 35);
        add(emailField);
        emailField.setBounds(200, 405, 160, 35);
        add(passwordField);
        passwordField.setBounds(200, 205, 160, 35);
        add(repasswordField);
        repasswordField.setBounds(200, 255, 160, 35);

        //---- registButton ----
        registButton.setText("\u6ce8\u518c");
        registButton.addActionListener(e -> regist(e));
        add(registButton);
        registButton.setBounds(50, 470, 105, 35);

        //---- resetButton ----
        resetButton.setText("\u91cd\u7f6e");
        resetButton.addActionListener(e -> reset(e));
        add(resetButton);
        resetButton.setBounds(190, 470, 105, 35);

        setPreferredSize(new Dimension(465, 590));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel titleLabel;
    private JButton backButton;
    private JLabel typeLabel;
    private JLabel nameLabel;
    private JLabel pwdLabel;
    private JLabel repwdLabel;
    private JLabel personIdLabel;
    private JLabel addrLabel;
    private JLabel emailLabel;
    private JComboBox<String> comboBox;
    private JTextField nameField;
    private JTextField personIdField;
    private JTextField addrField;
    private JTextField emailField;
    private JPasswordField passwordField;
    private JPasswordField repasswordField;
    private JButton registButton;
    private JButton resetButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
